﻿//#region 全局辅助函数
//词典中，获取关键字对应的值
function l(k) { return _helper.dict(k); }
//#endregion

//#region Helper
var _helper = new Helper();
function Helper()
{
	var __bToE = new BaseToExtend();

	function __init()
	{
		document.write('<style>img{max-width:' + window.screen.availWidth + 'px}</style>');
	}

	this.convert = function (baseText)
	{
		return __bToE.convert(baseText);
	};

	this.dict = function (key)
	{
		if (typeof (key) != 'string')
		{
			if (Ext.query('html')[0].lang != 'UYG')
				return key;

			if (Ext.isArray(key))
			{
				for (var i = 0; i < key.length; i++)
				{
					for (var name in key[i])
					{
						if (typeof (key[i][name]) == 'string')
							key[i][name] = __bToE.convert(key[i][name]);
					}
				}
			}
			else
			{
				for (var name in key)
				{
					if (typeof (key[name]) == 'string')
						key[name] = __bToE.convert(key[name]);
				}
			}

			return key;
		}
		else
		{
			for (var name in _dict)
			{
				if (key == name)
				{
					if (Ext.query('html')[0].lang == 'UYG')
						return __bToE.convert(_dict[name]);
					else
						return _dict[name];
				}
			}
		}
	};

	this.isLogin = function ()
	{
		if (__cookie('UserID') != '' && __cookie('Namespace') != 'AliCMS.SystemModule' && __cookie(__cookie('UserID')) != '')
			return true;
		else
			return false;
	};

	this.cookie = function (name, value, expireMinutes, path)
	{
		return __cookie(name, value, expireMinutes, path);
	};

	this.createEditor = function(container, selector, value)
	{
		//创建html编辑器
		var id = Ext.select(selector).elements[0].getAttribute('id');

		//参数
		var param = {toolbar:['fullscreen', 'undo', 'redo', 'bold', 'italic', 'underline'],initialFrameWidth:container.getWidth()};

		var editor = UM.getEditor(id, param);
		editor.ready(function ()
		{
			editor.setContent(value);
		});
	}

	function __cookie(name, value, expireMinutes, path)
	{
		if (value == null)
		{
			var array = document.cookie.split(';');
			for (var i = 0; i < array.length; i++)
			{
				var kvp = array[i].split('=');
				if (kvp.length != 2)
					continue;

				var j = 0;
				for (; j < kvp[0].length; j++)
				{
					if (kvp[0].charAt(j) != ' ')
						break;
				}

				if (name == kvp[0].substr(j))
					return unescape(kvp[1]);
			}
			return '';
		}
		else
		{
			if (path)
				path = ';path=' + path;
			else
				path = '';

			var expireDate = new Date();
			if (value == '')
				expireDate.setYear(expireDate.getFullYear() - 1);
			else
				expireDate.setMinutes(expireDate.getMinutes() + expireMinutes);
			document.cookie = name + '=' + escape(value) + ';expires=' + expireDate.toUTCString() + path;
		}
	}

	__init();
}
//#endregion
